package org.itzheng.yuezhi.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import org.itzheng.yuezhi.R;

/**
 * Title:<br>
 * Description: <br>
 * Company: <br>
 *
 * @author ZhengYongdong
 * @email ItZheng@ZoHo.com
 * @date 2016/6/24 0024
 */
public abstract class BaseFragment extends Fragment {
    FrameLayout fl_loading_layout;
    FrameLayout fl_base_content;
    Handler mainHandler = new Handler(Looper.getMainLooper());

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.base_layout, null);
        fl_loading_layout = (FrameLayout) view.findViewById(R.id.fl_loading_layout);
        fl_base_content = (FrameLayout) view.findViewById(R.id.fl_base_content);
        View contentView = onCreateView(inflater, container);
        if (contentView == null) {

        } else {
            if (contentView.getParent() == null) {

            } else {
                //如果有父类,则从父类中移除
                ((ViewGroup) (contentView.getParent())).removeView(contentView);
            }
            fl_base_content.addView(contentView);
        }
        return view;
    }

    /**
     * 切换到主线程
     *
     * @param runnable
     */
    public void runOnUiThread(Runnable runnable) {
        if (runnable == null) {

        } else {
            mainHandler.post(runnable);
        }
    }

    protected abstract View onCreateView(LayoutInflater inflater, ViewGroup container);

    public void showProgressDialog() {
        if (fl_loading_layout == null) {

        } else {
            fl_loading_layout.setVisibility(View.VISIBLE);
        }
    }

    public void dismissProgressDialog() {
        if (fl_loading_layout == null) {

        } else {
            fl_loading_layout.setVisibility(View.GONE);
        }
    }

}
